package com.dc.bobaoge.project.v3.to.v2.business.core.util;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.dc.bobaoge.base.entity.message.common.po.MessageAsset;
import com.dc.bobaoge.base.mapper.message.MessageAssetMapper;
import com.dc.bobaoge.common.base.entity.message.business.BaseMessageBusinessPo;
import com.dc.bobaoge.common.em.MessageTemplateEnum;
import com.dc.bobaoge.common.util.AssetResolveUtil;
import com.dc.bobaoge.project.base.core.constant.BaseInfoConstant;
import com.dc.bobaoge.project.base.core.old.entity.OldCoreMessage;
import com.own.component.common.exception.BusinessSimpleException;
import com.own.component.common.util.list.ListUtil;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.stream.Collectors;

/**
 * V3ToV2MessageCommonUtil
 *
 * @author chenxueli
 * @date 2024-01-13 09:20:00
 */
@Component
public class V3ToV2MessageCommonUtil {

    @Resource
    private MessageAssetMapper messageAssetMapper;

    /**
     * 设置消息状态信息
     * <p>
     * 旧消息状态 0未处理 1已通过 2未通过 3管未审用户删 4管已审用户删 5管未审管理删 6管已审管理删
     * <p>
     * 新消息状态 1=待审核 2=审核通过 3=审核不通过 4=完成 5=已违规 6=锁定 7=待支付
     * <p>
     * 广告状态(0审核中,1正在进行，2手动结束投放，3管理员拒绝,4管理员删除,5已结束，6未开始，7不是购买当天的广告，9没支付)
     *
     * @param message        消息信息
     * @param oldCoreMessage 旧消息信息
     */
    public void setStatus(BaseMessageBusinessPo message, OldCoreMessage oldCoreMessage) {
        var status = message.getAuditStatus();
        var isDeleted = message.getIsDeleted();
        switch (status) {
            case BaseMessageBusinessPo.AuditStatus.WAIT,
                    BaseMessageBusinessPo.AuditStatus.LOCK,
                    BaseMessageBusinessPo.AuditStatus.WAIT_PAY:
                oldCoreMessage.setMessageState(isDeleted == 0 ? 0 : 5);
                break;
            case BaseMessageBusinessPo.AuditStatus.PASS:
                oldCoreMessage.setMessageState(isDeleted == 0 ? 1 : 6);
                break;
            case BaseMessageBusinessPo.AuditStatus.NOT_PASS:
                oldCoreMessage.setMessageState(isDeleted == 0 ? 2 : 6);
                break;
            case BaseMessageBusinessPo.AuditStatus.FINISH:
                oldCoreMessage.setMessageState(isDeleted == 0 ? 4 : 6);
                break;
            case BaseMessageBusinessPo.AuditStatus.VIOLATION:
                oldCoreMessage.setMessageState(6);
                break;
            default:
                throw new BusinessSimpleException("状态错误");
        }
    }

    /**
     * 设置媒资信息
     *
     * @param message        消息信息
     * @param oldCoreMessage 旧消息信息
     * @param template       模板信息
     */
    public void setMessageAssets(BaseMessageBusinessPo message, OldCoreMessage oldCoreMessage, MessageTemplateEnum template) {
        var imageUrls = getImageUrls(message.getId(), message.getSharding(), template);
        var instance = AssetResolveUtil.getInstance();
        // 进行分隔
        var urls = ListUtil.Spilt.to(imageUrls, ",").stream()
                .map(instance::resolve)
                .collect(Collectors.joining(BaseInfoConstant.IMAGE_SPILT));
        oldCoreMessage.setMessageImages(urls);
    }

    /**
     * 获取图片信息
     *
     * @param messageId 消息id
     * @param quarter   季度
     * @param template  模板信息
     * @return 图片信息
     */
    public String getImageUrls(Long messageId, String quarter, MessageTemplateEnum template) {
        // 查询消息的媒资信息
        var wrapper = new LambdaUpdateWrapper<MessageAsset>()
                .eq(MessageAsset::getFkMessageId, messageId)
                .eq(MessageAsset::getSharding, quarter)
                .eq(MessageAsset::getType, template.getType());
        var po = messageAssetMapper.selectOne(wrapper);
        return po == null ? "" : po.getImageUrls();
    }

    /**
     * 设置公共字段
     *
     * @param message        消息信息
     * @param oldCoreMessage 旧消息信息
     */
    public void setPublicField(BaseMessageBusinessPo message, OldCoreMessage oldCoreMessage) {
        oldCoreMessage.setMessageId(message.getId());
        oldCoreMessage.setMessageUserId(message.getFkUserId());
        oldCoreMessage.setMessageZoneId(message.getFkZoneId());
        oldCoreMessage.setMessageType(message.getFkTitleId().intValue());
        oldCoreMessage.setMessageContent(message.getContent());
        oldCoreMessage.setMessagePhone(message.getRfContactPhone());
        oldCoreMessage.setMessageDate(message.getPublishTime());
        oldCoreMessage.setMessageVerifyDate(message.getAuditTime());
        oldCoreMessage.setMessageAds(message.getAddress());
        oldCoreMessage.setMessageReason(message.getAuditRemark());
    }

}
